<template>
  <el-card class="register-card">
    <div slot="header" class="register-header">
      <h2>用户注册</h2>
    </div>
    <el-form
      ref="registerForm"
      :model="registerForm"
      :rules="rules"
      label-width="80px"
      @keyup.enter="handleRegister"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="registerForm.username"
          placeholder="请输入用户名"
          @keyup.enter.native="handleRegister"
        ></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="registerForm.email"
          placeholder="请输入邮箱"
          @keyup.enter.native="handleRegister"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="registerForm.password"
          placeholder="请输入密码"
          @keyup.enter.native="handleRegister"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          type="password"
          v-model="registerForm.confirmPassword"
          placeholder="请确认密码"
          @keyup.enter.native="handleRegister"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleRegister">注册</el-button>
        <el-button @click="resetForm('registerForm')">重置</el-button>
      </el-form-item>
      <el-form-item>
        <span>已有账号？</span>
        <el-button type="text" @click="$router.push('/auth/login')"
          >立即登录</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
  export default {
    data() {
      const validateConfirmPassword = (rule, value, callback) => {
        if (value !== this.registerForm.password) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      };

      return {
        registerForm: {
          username: "",
          email: "",
          password: "",
          confirmPassword: "",
        },
        rules: {
          username: [
            { required: true, message: "请输入用户名", trigger: "blur" },
            {
              min: 3,
              max: 20,
              message: "用户名长度在 3 到 20 个字符",
              trigger: "blur",
            },
          ],
          email: [
            { required: true, message: "请输入邮箱", trigger: "blur" },
            {
              type: "email",
              message: "请输入正确的邮箱格式",
              trigger: ["blur", "change"],
            },
          ],
          password: [
            { required: true, message: "请输入密码", trigger: "blur" },
            { min: 6, message: "密码长度不能少于 6 个字符", trigger: "blur" },
          ],
          confirmPassword: [
            { required: true, message: "请确认密码", trigger: "blur" },
            { validator: validateConfirmPassword, trigger: "blur" },
          ],
        },
      };
    },
    methods: {
      async handleRegister() {
        this.$refs.registerForm.validate(async (valid) => {
          if (valid) {
            try {
              // eslint-disable-next-line no-unused-vars
              const { ...registerData } = this.registerForm;
              const res = await this.$axios.post(
                "/auth/register",
                registerData
              );
              // console.log(res);
              if (res.code === 200) {
                this.$message.success("注册成功，请登录");
                this.$router.push("/auth/login");
              } else {
                this.$message.error(res.message);
              }
            } catch (error) {
              this.$message.error(error.response?.data?.message || "注册失败");
            }
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
  };
</script>

<style scoped>
  .register-card {
    max-width: 400px;
    margin: 80px auto;
  }

  .register-header {
    text-align: center;
  }
</style>
